﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace Kiosk
{
    /// <summary>
    /// Interaction logic for UserFavorites.xaml
    /// </summary>
    public partial class FavoriteMentors : Window
    {
        private List<Mentor> favoriteMentors;
        private DataSet dsFavoriteMentors; //the returned dataset from the SQL query
        private int returnCount = 0; //the amount of rows returned from the query.
        public FavoriteMentors()
        {
            InitializeComponent();
            displayMentors();
        }

        /// <summary>
        /// This method displays the mentors in the Listbox.
        /// </summary>
        private void displayMentors()
        {
            //Reset the favoriteMentors list and dataset
            favoriteMentors = new List<Mentor>();
            dsFavoriteMentors = new DataSet();
            lbFavoriteMentors.ItemsSource = null;

            string sqlQuery = "SELECT fm.favorite_mentor_id, m.mentor_id, CONCAT_WS(' ', m.first_name, m.last_name) AS fullname, d.degree_name, p.pathway_name, m.photo " +
                                "FROM favorite_mentors AS fm " +
                                "INNER JOIN mentor AS m ON m.mentor_id = fm.mentor_id " +
                                "INNER JOIN lkup_pathway AS p ON p.pathway_id = m.pathway_id " +
                                "INNER JOIN lkup_degree AS d ON d.degree_id = m.degree " +
                                "WHERE fm.user_id = (SELECT user_id FROM users WHERE username = '" + sGlobals.currentUsername + "') AND m.deletion_date IS NULL";
            try
            {
                dsFavoriteMentors = sGlobals.dbWrapper.ExecuteSQLStatement(sqlQuery, ref returnCount);

                //If there were FavoriteMentors returned, then we display them. Else we display a message that the user has no
                //Favorite Mentors selected.
                if (returnCount > 0)
                {
                    lblTitle.Content = "FAVORITE MENTORS";
                    lblcount.Content = returnCount.ToString() + " RESULT(S)";
                    for (int i = 0; i < dsFavoriteMentors.Tables[0].Rows.Count; i++)
                    {
                        if (((byte[])dsFavoriteMentors.Tables[0].Rows[i]["photo"]).Count() == 0)
                        {
                            favoriteMentors.Add(new Mentor(
                                dsFavoriteMentors.Tables[0].Rows[i]["fullname"].ToString(),
                                dsFavoriteMentors.Tables[0].Rows[i]["degree_name"].ToString(),
                                dsFavoriteMentors.Tables[0].Rows[i]["pathway_name"].ToString(),
                                dsFavoriteMentors.Tables[0].Rows[i]["mentor_id"].ToString()));
                        }
                        else
                        {
                            favoriteMentors.Add(new Mentor(
                                dsFavoriteMentors.Tables[0].Rows[i]["fullname"].ToString(),
                                dsFavoriteMentors.Tables[0].Rows[i]["degree_name"].ToString(),
                                dsFavoriteMentors.Tables[0].Rows[i]["pathway_name"].ToString(),
                                (byte[])dsFavoriteMentors.Tables[0].Rows[i]["photo"],
                                dsFavoriteMentors.Tables[0].Rows[i]["mentor_id"].ToString()));
                        }
                    }
                    lbFavoriteMentors.ItemsSource = favoriteMentors;
                }
                else
                {
                    lblcount.Content = "0 RESULT(S)";
                }
            }
            catch (Exception ex)
            {
                if (!sGlobals.hasLogout)
                {
                    WarningMessage warning = new WarningMessage();
                    warning.lblMessage.Content = "There was a problem loading your saved mentors.\r\nPlease contact your system administrator";
                    warning.ShowDialog();
                    lblcount.Content = "0 RESULT(S)";
                }
            }
        }//end method displayMentors

        /// <summary>
        /// This method is called when one of the RemoveFavMentor buttons is pressed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveFavMentor_Click(object sender, RoutedEventArgs e)
        {
            Button btnPressed = (Button)sender;
            string mentor_id = (string)btnPressed.DataContext;
            string sqlQuery = "DELETE FROM favorite_mentors " +
                              "WHERE user_id = (SELECT user_id FROM users WHERE username = '" + sGlobals.currentUsername + "') " +
                              "AND mentor_id = '" + mentor_id + "'";
            try
            {
                int affectedRows = sGlobals.dbWrapper.ExecuteNonQuery(sqlQuery);
                if (affectedRows > 0)
                {
                    displayMentors();
                }
                else
                {
                    WarningMessage warning = new WarningMessage();
                    warning.lblMessage.Content = "There was a problem deleting the mentor. Please try again in a few minutes.";
                    warning.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                if (!sGlobals.hasLogout)
                {
                    WarningMessage warning = new WarningMessage();
                    warning.lblMessage.Content = "There was a problem deleting the mentor. Please try again in a few minutes.";
                    warning.ShowDialog();
                }
            }
        }

        /// <summary>
        /// This method logs the user out of the kiosk. It is called when the user presses the logout button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogout_Click(object sender, RoutedEventArgs e)
        {
            sGlobals.logout();
        }

        /// <summary>
        /// This method removes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveAllFavs_Click(object sender, RoutedEventArgs e)
        {
            string sqlQuery = "DELETE FROM favorite_mentors " +
                              "WHERE user_id = (SELECT user_id FROM users WHERE username = '" + sGlobals.currentUsername + "') ";
            try
            {
                int affectedRows = sGlobals.dbWrapper.ExecuteNonQuery(sqlQuery);
                if (affectedRows > 0)
                {
                    displayMentors();
                }
                else
                {
                    WarningMessage warning = new WarningMessage();
                    warning.lblMessage.Content = "There was a problem deleting the mentors";
                    warning.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                if (!sGlobals.hasLogout)
                {
                    WarningMessage warning = new WarningMessage();
                    warning.lblMessage.Content = "There was a problem deleting the mentors";
                    warning.ShowDialog();
                }
            }
        }

        private void _listBoxItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            AdvisorBox adBox = (AdvisorBox)sender;
            sGlobals.selectedMentorID = adBox.mentorID.Content.ToString();
            sGlobals.mentorDisplay.previousPage = this;

            string viewMentorLogQuery = "INSERT INTO lgd_mentor_student_actions (mentor_id, student_id, date_time, " +
                    "action_performed_id) VALUES ('" + sGlobals.selectedMentorID + "', (SELECT user_id FROM users WHERE username = '" +
                    sGlobals.currentUsername + "'), NOW(), 'V')";
            try
            {
                sGlobals.dbWrapper.ExecuteNonQuery(viewMentorLogQuery);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error connecting to the database with Exception: {0}", ex.Message);
            }
            sGlobals.mentorDisplay.Show();
            this.Hide();
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            sGlobals.focusedAreasOfStudy.Show();
            this.Hide();
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            displayMentors();
        }
    }
}
